package com.leetcode.algorithm.y22.m07;

/**
 * 724. 寻找数组的中心下标
 * 
 * https://leetcode.cn/problems/find-pivot-index/
 * 
 * @author jie.deng
 *
 */
class Question0724Solution01 {
	
	public int pivotIndex(int[] nums) {
		int len = nums.length;
		int[] prefixSum = new int[len];
		prefixSum[0] = nums[0];
		for (int i = 1; i < prefixSum.length; i++) {
			prefixSum[i] = prefixSum[i - 1] + nums[i];
		}
		for (int i = 0; i < len; i++) {
			int left = (i == 0 ? 0 : prefixSum[i - 1]);
			int right = (i == len - 1 ? 0 : prefixSum[len - 1] - prefixSum[i]);
			if (left == right) {
				return i;
			}
		}
		return -1;
	}
    
}